﻿using System;
using System.Collections.Generic;
using System.Linq;
using WickedFlame.Core;
using WickedFlame.Core.Shapes;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Media;

namespace WickedFlame.Util.Actions
{
    public class MergeImagesAction
    {
        public void DoAction(DrawingCanvas canvas)
        {
            // convert all shapes to images
            ConvertShapeToImageAction imgaction = new ConvertShapeToImageAction();
            imgaction.DoAction(canvas);

            var images = from ImageShape child in canvas.SelectedLayer.ShapeList
                         where child.IsSelected && child is ImageShape
                         select child;

            if (images.Count() > 0)
            {
                // bounding rectangle of the new image
                double x = canvas.Width;
                double y = canvas.Height;
                double bottom = 0;
                double right = 0;

                foreach (VectorShape s in canvas.SelectedLayer.ShapeList.Selection)
                {
                    if (s.Bounds.X < x)
                        x = s.Bounds.X;
                    if (s.Bounds.Y < y)
                        y = s.Bounds.Y;
                    if (s.Bounds.Right > right)
                        right = s.Bounds.Right;
                    if (s.Bounds.Bottom > bottom)
                        bottom = s.Bounds.Bottom;
                }

                Rect bounds = new Rect(x, y, right - x, bottom - y);


                //offset to new position where upermost image is in position 0,0
                double offx = -bounds.X;
                double offy = -bounds.Y;
                //reposition shapes in a new rectangle
                foreach (ImageShape shape in images)
                {
                    shape.Move(offx, offy);
                }


                // merge all the copied shapes and
                // draw the new image from the shapes
                DrawingVisual dvbase = new DrawingVisual();
                DrawingContext dc = dvbase.RenderOpen();

                foreach (ImageShape shape in images)
                {
                    shape.Draw(dc);
                }
                dc.Close();

                // Create a render bitmap and push the surface to it
                RenderTargetBitmap renderBitmap =
                  new RenderTargetBitmap(
                    (int)bounds.Width,
                    (int)bounds.Height,
                    96d,
                    96d,
                    System.Windows.Media.PixelFormats.Pbgra32);

                renderBitmap.Render(dvbase);

                ConvertImageToPbgra32Action imconv = new ConvertImageToPbgra32Action();
                BitmapImage image = imconv.ConvertBitmapFormat(renderBitmap);

                var mergedImg = new ImageShape(image, bounds.X, bounds.Y);

                while (images.Count() > 0)
                {
                    canvas.RemoveShape(images.First());
                }
                //foreach (ImageShape shape in images)
                //{
                //    canvas.RemoveShape(shape);
                //}

                canvas.AddShape(mergedImg);
                mergedImg.Invalidate();
            }
            //DrawingVisual cddv = new DrawingVisual();
            //DrawingContext cbdc = cddv.RenderOpen();
            //cbdc.DrawImage(image, new Rect(0, 0, bounds.Width, bounds.Height));
            //cbdc.Close();

            //// Create a render bitmap and push the surface to it
            //RenderTargetBitmap rb2 =
            //  new RenderTargetBitmap(
            //    (int)bounds.Width,
            //    (int)bounds.Height,
            //    96d,
            //    96d,
            //    System.Windows.Media.PixelFormats.Pbgra32);

            //rb2.Render(cddv);

            //BitmapImage cbimg = imconv.ConvertBitmapFormat(rb2);

        }
    }
}
